import br.unifor.finance.business.facade.test.TesteFacadeLocal;
import br.unifor.finance.common.business.entity.Teste;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/*
 * TestMain.java
 *
 * Created on 8 de Julho de 2007, 16:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Clebernaice, Cleilson, Leonardo
 */
public class TestMain {
  
  /** Creates a new instance of TestMain */
  public TestMain() {
  }
  
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    TestMain main = new TestMain();
    
    Teste teste = new Teste();
    teste.setNome("Teste Finance EJB");
    teste.setDescricao("Teste Finance EJB");
    
    TesteFacadeLocal facade = main.lookupTesteFacade();
    facade.create(teste);
    
    List list = facade.findAll();
    for (Iterator it = list.iterator(); it.hasNext();) {
      Teste elem = (Teste) it.next();
      System.out.println(elem);
      System.out.println("\tId: " + elem.getId());
      System.out.println("\tNome: " + elem.getNome());
      System.out.println("\tDescricao: " + elem.getDescricao());
    }
  }

  private DataSource getFinanceTest() throws NamingException {
    Context c = new InitialContext();
    //return (DataSource) c.lookup("java:comp/env/financeTest");
    return (DataSource) c.lookup("java:/financeTestDS");
  }

  private TesteFacadeLocal lookupTesteFacade() {
    try {
      Hashtable t = new Hashtable();
      t.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      t.put(Context.PROVIDER_URL, "localhost");
      t.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      Context c = new InitialContext(t);
      //Context c = new InitialContext();
      return (TesteFacadeLocal) c.lookup("finance/TesteFacade/local");
      //Context c = new InitialContext();
      //return (TesteFacadeLocal) c.lookup("java:comp/env/ejb/TesteFacade");
    }
    catch(NamingException ne) {
      Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
      throw new RuntimeException(ne);
    }
  }

}
